package org.uim.cricket.storage.raise.fetch;

import java.io.File;

import org.uim.cricket.entities.match.Ground;
import org.uim.cricket.storage.StorageProperties;

public class GroundArgumentBuilder implements FetchArgumentBuildable {
	
	private int status = 0;
	
	public GroundArgumentBuilder() {
		status = 0;
	}
	
	@Override
	public String buildArgument(Object obj) throws Exception {
		Ground g = (Ground) obj;
		
		if (status % 2 == 0) {
			status++;
			String dir = StorageProperties.getValue("repo.fs.grounds.dir");
			File f = new File(dir);
			if (dir == null || !f.exists()) {
				throw new IllegalArgumentException (" Can't find a FileSystem resource location for ground!");
			} else if (!f.isDirectory()) {
				throw new IllegalArgumentException (" Resource location is not a directory!");
			}
			
			if (!dir.endsWith("/")) {
				dir += "/";
			}
			
			if (g.getId() == null) {
				throw new NullPointerException (" Ground id can't be null!");
			} else {
				return dir + g.getId() + ".html";
			}
			
		} else {
			
			String url = StorageProperties.getValue("repo.web.grounds.url");
			if (url == null) {
				throw new IllegalArgumentException (" Can't find a URL resource location for ground!");
			}
			if (!url.endsWith("/")) url += "/";
			
			return url + g.getId() + ".html";
		}
	}

}
